package cz.muni.fi.pa165.musiclibrary.impl.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.Validate;
import org.springframework.transaction.annotation.Transactional;

import cz.muni.fi.pa165.musiclibrary.impl.database.dao.IGenreDAO;
import cz.muni.fi.pa165.musiclibrary.impl.database.entity.IGenre;
import cz.muni.fi.pa165.musiclibrary.api.service.IGenreService;
import cz.muni.fi.pa165.musiclibrary.api.service.dto.IGenreDTO;
import cz.muni.fi.pa165.musiclibrary.impl.service.dto.translator.GenreDTOTranslator;

/**
*
* @author Radoslav Stefanik 
* 2012-10-30T22:19:59+0100
*/
@Transactional
public final class GenreService implements IGenreService{

    private IGenreDAO genreDAO;
    
    public GenreService(final IGenreDAO genreDAO) {
        this.genreDAO = genreDAO;
    }
    
    @Override
    public void create(IGenreDTO genre, String userName) {
        Validate.isTrue(genre!=null, "Genre should not be null");
        genreDAO.create(GenreDTOTranslator.translateFromDTO(genre));
    }

    @Override
    public void update(IGenreDTO genre, String userName) {
        Validate.isTrue(genre!=null, "Genre should not be null");
        genreDAO.update(GenreDTOTranslator.translateFromDTO(genre));
    }

    @Override
    public void delete(IGenreDTO genre, String userName) {
        Validate.isTrue(genre!=null, "Genre should not be null");
        genreDAO.delete(GenreDTOTranslator.translateFromDTO(genre));
    }

    @Override
    public IGenreDTO getById(Long idGenre) {
        Validate.isTrue(idGenre!=null, "Genre id should not be null");
        return GenreDTOTranslator.translateToDTO(genreDAO.getById(idGenre));
    }

    @Override
    public List<IGenreDTO> getAll() {
        List<IGenreDTO> allGenresDTO = new ArrayList<IGenreDTO>();
        List<IGenre> allGenres = genreDAO.getAll();
        for(IGenre genre : allGenres) {
            allGenresDTO.add(GenreDTOTranslator.translateToDTO(genre));
        }
        return allGenresDTO;
    }

}